
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:jubobao/home/JuBoHomeModel.dart';
import 'package:jubobao/home/JuBoNews.dart';
import 'package:jubobao/home/JuBo_home_services.dart';
import 'package:jubobao/other/Widgets/JuBoCommonWidget.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'dart:ui';

import 'JuBoNewWebView.dart';

class JuBoHomePage extends StatefulWidget {
  @override
  _JuBoHomePageState createState() => _JuBoHomePageState();
}

class _JuBoHomePageState extends State<JuBoHomePage> with AutomaticKeepAliveClientMixin {

  Result _result;
   @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    JuBoHomeRequest.homeConfig().then((value) {
        print(value.d.result.navigationBar);
        setState(() {
            _result = value.d.result;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    return _result == null ? JuBoWidget.loadingWidget() :  homeContent();
  }

  Widget homeContent (){
    return CustomScrollView(
      slivers: <Widget>[banner(), menus(), consulting(), curtainWallTender()],
    );
  }

  Widget banner (){
    return SliverAppBar (
      pinned: true,
      stretch: true,
      expandedHeight: ScreenUtil().setHeight(250),
      flexibleSpace: FlexibleSpaceBar(
        title: Text('首页',style: TextStyle(fontSize: 18),),
        background: Swiper(
            itemCount: _result.banners.length,
            itemBuilder: (ctx,index){
              final banner = _result.banners[index];
              return Image.network(banner.image,fit: BoxFit.cover);
            },
            pagination : SwiperPagination(),
            autoplay : true,
            onTap: (index){
              final banner = _result.banners[index];
              Navigator.of(context).push(MaterialPageRoute(
                  builder: (ctx){
                    return JuBoNewWebView(banner.link);
                  }
              ));
            },
        ),
      ),
    );
  }

  Widget menus(){
    return SliverGrid(

      delegate: SliverChildBuilderDelegate((ctx,index){
        return menuItem(index);
      },
          childCount: _result.navigationBar.length
      ),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
          childAspectRatio: 1.0
      ),
    );
  }

  Widget menuItem(index){

     final banner = _result.navigationBar[index];
     return Padding(
       padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
       child: Column(
         children: <Widget>[
           Image.network(
             banner.image,
             width: ScreenUtil().setWidth(80),
           ),
           Text(banner.name),
         ],
       ),
     );
   }

  Widget consulting (){
    return SliverList(
        delegate: SliverChildBuilderDelegate((ctx,index){
          return Container(
            decoration: BoxDecoration(
              border: Border(
                top: BorderSide(
                  width: 8,
                  color: Color.fromARGB(255, 225, 225, 225),
                ),
                bottom: BorderSide(
                  width: 8,
                  color: Color.fromARGB(255, 225, 225, 225),
                ),
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 8),
              child: Column(
                children: <Widget>[
                  consultingItem(),
                  SizedBox(height: 8,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      SizedBox(width: 8,),
                      Expanded(child: warehousing(0)),
                      SizedBox(width: 8,),
                      Expanded(child: warehousing(1)),
                      SizedBox(width: 8,)
                    ],
                  ),
                  SizedBox(height: 10,),
                  monopoly(),
                ],
              ),
            ),
          );
        },
        childCount: 1
        ),
    );
  }

  Widget consultingItem(){

    final information = _result.juboInformation[0];

    return InkWell(
      onTap: (){
          Navigator.of(context).pushNamed(JuBoNews.routeName);
      },
      child: Row(
        children: <Widget>[
          SizedBox(width: 8,),
          Image.asset('assets/home/jubo_consulting.png',width: 25,),
          SizedBox(width: 8,),
          Expanded(
            child: Text(
              information.description,
              maxLines: 2,
            ),
          )
        ],
      ),
    );
  }
  // 仓储系统
  Widget warehousing(index){
    final friendDealer = _result.friendDealer[index];
    return InkWell(
      child: Container(
        height: ScreenUtil().setHeight(150),
        child: Stack(
             alignment : Alignment.center,
             children: <Widget>[
             Image.network(friendDealer.image,fit: BoxFit.fill),
             Text(friendDealer.name,style: TextStyle(color: Colors.white,fontSize: 18),),
             Positioned(child: Text(friendDealer.description,style: TextStyle(color: Colors.white),),bottom: 10,),
            ],
        ),
      ),
    );
  }

  Widget monopoly (){
    final bigNameMonopoly = _result.bigNameMonopoly[0];
      return Column(
        children: <Widget>[
          monopolyTitle(bigNameMonopoly.name,bigNameMonopoly.description),
          monopolyContent(bigNameMonopoly.child)
        ],
      );
  }

  Widget monopolyTitle (String title,String content){
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal:8),
      child: Row(
        children: <Widget>[
            Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Image.asset('assets/home/icon_sy_xmzb.png',width: 80,height: 30,),
                Text(title,style: TextStyle(color: Colors.white),),
              ],
            ),
             SizedBox(width: 10,),
             Text(content),
        ],
      ),
    );
  }

  Widget monopolyContent (child){
    return Row(
      children: <Widget>[
        SizedBox(width: 10,),
        Expanded(
           child: Image.network(
             child[0].image,
           ),
         ),
        SizedBox(width: 10,),
        Expanded(
           child: Column(
             children: <Widget>[
               Image.network(child[1].image),
               SizedBox(height: 8,),
               Image.network(child[2].image),
             ],
           ),
         ),
        SizedBox(width: 10,),
      ],
    );
  }

  Widget curtainWallTender (){

    final projectBidding = _result.projectBidding[0];

    return SliverGrid(
        delegate: SliverChildBuilderDelegate((ctx, index){

          final projectBiddingItem = projectBidding.child[index];

          return Padding(
            padding: const EdgeInsets.fromLTRB(5, 5, 5 , 5),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Stack(
                  children: <Widget>[
                    Image.network(projectBiddingItem.image),
                    Positioned(child: Image.asset('assets/home/icon_sy_zb.png',width: 60,),top: 5,),
                    Positioned(child: Text('招标中',style: TextStyle(color: Colors.white),),top: 5,left: 5,)
                  ],
                ),
                SizedBox(height: 5,),
                Text(projectBiddingItem.name,style: TextStyle(fontSize: 15),)
              ],
            ),
          );
        },
        childCount: projectBidding.child.length
        ),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
        ),
    );
  }
}



